const App = {
  mixins: [Query], //配置文件，路径/utils/mixins.js
  data() {
    return {
      value: "",
      QR_code: false,
      qr: null, // 二维码生成器实例
      currentActivity: null, // 当前活动
      szText: "",
      year: new Date().getFullYear(),
      month: "",
      page: {
        activeName: 'first',
        value1: '',
        value2:'',
        PageNumber: 1,
        RowAmount: 10,
        Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%"))),
        ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND VillageNumber IN (" + getCookie('village') + ")\r\n"))),
      },
      str1:'',
      str2:"AND VillageNumber IN (" + getCookie('village') + ")\r\n",
    };
  },
  mounted() {

  },
  methods: {
    //查询按钮
    check() {
      this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%,%%,%"+(this.page.value2 || '') +"%")));
      this.postgetList(this.page); // 调用查询方法
    },
    handleClick() {
      if (this.page.activeName == 'first') {
        this.str1 = "";
      } else if (this.page.activeName == 'second') {
        this.str1 = "AND village_activities.Activity_Datetime > NOW()\r\n";
      } else if (this.page.activeName == 'third') {
        this.str1 = "AND NOW() BETWEEN village_activities.Activity_Datetime AND village_activities.Activity_End_Datetime\r\n";
      } else if (this.page.activeName == 'fourth') {
        this.str1 = "AND village_activities.Activity_End_Datetime < NOW() \r\n";
      }
      this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent(this.str1 + this.str2)));
      this.postgetList(this.page)
    },
    handlexq(row) {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        method: "post",
        data: {
          Data: "party_three_app_account_manage", //表名
          Condition: "Row_Status='0'",
        },
        success: (data) => {
          console.log(data);
          console.log(data[0].AppID);
          sessionStorage.setItem("AppID", data[0].AppID);
          this.wha(data[0].AppID, row);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
      // return;
    },
    wha(AppID, row) {
      console.log(AppID, row);
      // return;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/weChatMiniprogram/getUrlLink`,
        method: "get",
        data: {
          AppID: AppID,
          Path: "villageResite/conferenceDetails/conferenceDetails",
          ExpireType: "1",
          Interval: "1",
          Query: `id=${row.Village_Activity_SerialNumber}`,
          Env_Version: "trial",
        },
        success: (data) => {
          console.log(data);
          if (data.errmsg == "ok") {
            this.erw(data.url_link, row);
          }
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    erw(arg_url, row) {
      this.currentActivity = row;
      this.QR_code = true;

      // 先验证URL有效性
      if (!this.validateURL(arg_url)) {
        this.$message.error("URL格式错误，无法生成二维码");
        return;
      }

      this.$nextTick(() => {
        const qrElementId = `qr-${row.Party_Organization_Activity_SerialNumber}`;
        const qrElement = document.getElementById(qrElementId);

        // 清空原有二维码（如果存在）
        if (qrElement) qrElement.innerHTML = "";

        // 优化后的二维码生成参数
        new QRious({
          element: qrElement,
          value: arg_url,
          size: 256, // 增大尺寸到256px
          level: "H", // 最高容错率（可恢复30%损坏）
          background: "#ffffff", // 明确背景色
          foreground: "#000000", // 明确前景色
          quietZone: 4, // 增加空白边距
        });
        // 生成成功后添加动画效果
        if (qrElement) {
          qrElement.style.transition = "all 0.5s ease";
          qrElement.style.opacity = "0";
          setTimeout(() => {
            qrElement.style.opacity = "1";
          }, 100);
        }
      });
    },
    huir(row) {
      let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
      window.location.href = "./show.html?nodeKey=" + row[primary_key];
    },
    // 在methods中添加新方法
    regenerateQRCode() {
      if (!this.currentActivity || !this.currentActivity.url) {
        this.$message.warning("当前活动信息不完整，无法重新生成二维码");
        return;
      }

      // 显示加载状态
      this.loading = true;

      // 模拟重新获取URL（实际应用中可能需要从服务器重新获取）
      setTimeout(() => {
        this.erw(this.currentActivity.url, this.currentActivity);
        this.$message.success("二维码已重新生成");
        this.loading = false;
      }, 500);
    },

    // URL有效性验证方法
    validateURL(url) {
      try {
        new URL(url);
        return true;
      } catch (e) {
        console.error("URL验证失败:", e);
        return false;
      }
    },
    //报名人员
    canh(row) {
      let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
      window.location.href = "./addbm.html?action=Modify&nodeKey=" + row[primary_key];
    },
    Treedata() {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/permissionsTree/getDistrictTreeByPermissions`,
        type: "GET",
        data: {
          DistrictCode: getCookie('village'), //请求参数
        },
        success: (data) => {
          this.treeData = data;
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleNodeClick(data) {
      this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%,%" + data.ProvinceNumber + "%,%"+(this.page.value2 || '')+"%")));
      this.postgetList(this.page); // 调用查询方法
    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
const ElMessageBox = ElementPlus.ElMessageBox;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
